package com.blockmeta.bbs.businesslibrary.net.interceptor;

import com.blockmeta.bbs.baselibrary.net.Host;
import com.blockmeta.bbs.businesslibrary.util.JWTRefreshUtil;

import java.io.IOException;

import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.internal.http.RealInterceptorChain;

//jwt失效后，需要请求服务器进行重置jwt，如果超时过久，无法重置就会登出
public class CodeHandleInterceptor implements Interceptor {
    @Override
    public Response intercept(Chain chain) throws IOException {
        RealInterceptorChain realChain = (RealInterceptorChain) chain;
        Request request = realChain.request();
        Response response = chain.proceed(request);
        boolean ourNetWork = Host.BASE_URL.contains(request.url().host());
        if (response.code()==401 && ourNetWork){
            JWTRefreshUtil.refreshJWT();
        }
        return response;
    }
}
